package com.testdemo.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.labmanager.JsonModule.RequestBase;
import com.labmanager.JsonModule.ResponseBase;
import com.testdemo.web.service.LabService;
import com.testdemo.web.vo.LabInfo;
import com.testdemo.web.vo.Session;

@Controller
@RequestMapping("/lab")
public class LabController {
	@Resource
	private LabService labService;
	
	@RequestMapping(value = "/getlabtype", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase GetLabtype(@RequestBody RequestBase requestBase) {
		ResponseBase responseBase = new ResponseBase();
		
		List<LabInfo> labinfolist = labService.getLabTypeList();
		
		responseBase.setData(labinfolist);
		
		
		return responseBase;
	}
	
	@RequestMapping(value = "/updatelab", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase UpdateLab(@RequestBody LabInfo labInfo) {
		ResponseBase responseBase = new ResponseBase();
		
		//检验数据合法性
		if(!labInfo.checkData()) {
			responseBase.setError(-1, "请求参数错误");
			return responseBase;
		}
		//验证用户权限
		 Session session = AccountController.sessionManager.selectSessionByAccessToken(labInfo.getSession());
		 if(session == null) {
			 responseBase.setError(-1, "查找用户信息失败");
			 return responseBase;
		 }
		 if(!labService.checkuserName(session.getUsername(), labInfo.getLabid().intValue())) {
			 responseBase.setError(1, "用户没有权限修改该实验室的信息");
			 return responseBase;
		 }
		 labService.updateLabinfo(labInfo);
		
		return responseBase;
	}
}
